﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Configuration;
using System.IO;

namespace PSE.Communication.Fix.Common
{
    public class FixConfigurationManager
    {
        private static FixConfigurationManager _instance;
        private static Mutex _mtx = new Mutex();

        private FixConfigurationManager()
        {

        }

        public static FixConfigurationManager GetInstance()
        {
            _mtx.WaitOne();

            try
            {
                if (_instance == null)
                    _instance = new FixConfigurationManager();
            }
            catch
            {
                throw;
            }
            finally
            {
                _mtx.ReleaseMutex();
            }

            return _instance;
        }

        internal static string GetFullPath(string path)
        {
            string fullPath = path;

            if (!string.IsNullOrEmpty(path) &&
                !string.IsNullOrEmpty(System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath) &&
                !Path.IsPathRooted(path))
            {
                string configPath = Path.GetDirectoryName(System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath) + "\\";

                fullPath = Path.GetFullPath(Path.Combine(configPath, path));

                if (!System.IO.Path.HasExtension(fullPath) && !System.IO.Directory.Exists(fullPath))
                    System.IO.Directory.CreateDirectory(fullPath);
            }

            return fullPath;
        }
    }
}
